/**
 * 
 */
package com.wisdytech.linkdcs.system.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.system.model.SysOperation;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.system.service.ISystemOperationService;
import com.wisdytech.linkdcs.system.vo.OperationVO;

/**
 * <p>
 * ClassName: SystemOperationAction
 * </p>
 * <p>
 * Description: 工序管理页面action层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-10-17
 * </p>
 */
@Controller
@RequestMapping("/system/operation")
public class SystemOperationAction {
    /**
     * 注入工序管理service
     */
    @Autowired
    @Qualifier("systemOperationService")
    private ISystemOperationService systemOperationService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "system/operation/operationIndex";
    }
    
    
    /**
     * 分页条件查询工序管理列表信息
     * @param code 工序编码
     * @param name 工序名称
     * @param filter 分页参数
     * @return 工序管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listSysOperationByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<OperationVO> listSysOperationByFilter(String code, String name, Filter filter) throws IOException{
    	PageInfo<SysOperation> operations =this.systemOperationService.listSysOperationByFilter(code, name, filter);
        //转换为前台结果集
        List<SysOperation> operationList=operations.getList();
        List<OperationVO> operationVOList=new ArrayList<OperationVO>();
        for(SysOperation operation:operationList){
            operationVOList.add(DataTransformUtil.changeOperationVO(operation));
        }
        return new StandardTableResult<OperationVO>(operations.getTotal(),operationVOList);
    }
    /**
     * 保存工序信息
     * @param sysOperation 工序对象
     * @param session 
     * @return 保存结果集
     */
    @RequestMapping(value = "/saveOperation/save.mvc")
    @ResponseBody
    public StandardResult saveOperation(SysOperation sysOperation, HttpSession session) {
        SysUser sysUser = BaseUtil.getCurrentUser();
        String loginName = sysUser.getUsername();
        if (StringUtils.isBlank(sysOperation.getPkId())) {
            return systemOperationService.addSysOperation(sysOperation, loginName);
        }else {
            return systemOperationService.updateSysOperation(sysOperation, loginName);
        }
    }
    /**
     * 通过id批量删除工序信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delSysOperationById/del.mvc")
    @ResponseBody
    public StandardResult delSysOperationById(String pkId) {
        return systemOperationService.delSysOperationById(pkId);
    }
    
    /**
     * 通过id获取当前工序信息
     * @param id 主键id
     * @return 前台工序显示对象
     */
    @RequestMapping(value = "/getSysOperationById/get.mvc")
    @ResponseBody
    public OperationVO getSysOperationById(String pkId) {
        SysOperation sysOperation = new SysOperation();
        sysOperation = systemOperationService.getSysOperationById(pkId);
        OperationVO operationVO = DataTransformUtil.changeOperationVO(sysOperation);
        return operationVO;
    }
    
    /**
     * <p>Description:查询工序信息列表</p> 
     * @return 工序信息列表集合
     * @throws IOException 
     */
    @RequestMapping(value = "/listAllOperation/get.mvc")
    @ResponseBody
    public List<SysOperation> listAllOperation(@ModelAttribute("search") String search) throws IOException{
        return systemOperationService.listAllOperation(search);
    }
    
}
